package com.lswms.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.lswms.common.page.PageInfo;
import com.lswms.common.page.PageRequest;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.common.utils.MoneyUtil;
import com.lswms.common.utils.ZqDateUtil;
import com.lswms.dao.YnDcUserDao;
import com.lswms.dto.YnDcUserDTO;
import com.lswms.dto.vo.resp.TradeListResp;
import com.lswms.entity.YnDcUserEntity;
import com.lswms.entity.YnPayRecordEntity;
import com.lswms.entity.YnUserEntity;
import com.lswms.service.YnDcUserService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户注册信息
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-07-24
 */
@Service
@Slf4j
public class YnDcUserServiceImpl extends CrudServiceImpl<YnDcUserDao, YnDcUserEntity, YnDcUserDTO> implements YnDcUserService {

    @Override
    public QueryWrapper<YnDcUserEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<YnDcUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    /**
     * 借款记录
     */
	@Override
	public PageInfo<TradeListResp> loanList(PageRequest dto, YnUserEntity ynuser) {
		log.info("借款记录，参数dto:{},ynuser:{}",JSON.toJSONString(dto),JSON.toJSONString(ynuser));
        QueryWrapper<YnDcUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("userid", ynuser.getId());
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", dto.getPageNo());
        params.put("limit", dto.getPageSize());

		IPage<YnDcUserEntity> pageInfo = baseDao.selectPage(getPage(params, "create_date", false),wrapper);
		
		List<TradeListResp> list = new ArrayList<TradeListResp>();
		for(YnDcUserEntity entity : pageInfo.getRecords()){
			TradeListResp resp = new TradeListResp();
			String mes = ZqDateUtil.yyyy_MM_dd(entity.getCreateDate())+"申请"+entity.getPname();
			resp.setTitle(mes);
			list.add(resp);
		}
		
		PageInfo<TradeListResp> pageList = new PageInfo<TradeListResp>();
		pageList.setCount(pageInfo.getTotal());
		pageList.setList(list);
		pageList.setPageNo(Long.parseLong(dto.getPageNo()));
		pageList.setPages(pageInfo.getPages());
		pageList.setPageSize(Long.parseLong(dto.getPageSize()));
		log.info("借款记录，结果:{}",JSON.toJSONString(pageList));
		return pageList;
	}


}